<template>
  <div>
    <input
      placeholder="输入关键字搜索"
      v-model.trim="doctorInfo"
      @keyup.enter="handleFilter"
      @input="fnInput"
    />
    <table class="my-table">
      <thead>
        <tr>
          <th>就诊日期</th>
          <th>医生姓名</th>
          <th>诊断结果</th>
          <th>处方信息</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody v-if="isShow">
        <tr v-for="(item, index) in list" :key="index">
          <td>{{ item.date }}</td>
          <td>{{ item.doctor }}</td>
          <td>{{ item.diagnosis }}</td>
          <td>{{ item.prescription }}</td>
          <td @click="fn(item.doctor)">详情</td>
        </tr>
      </tbody>
      <tbody v-else>
        <tr v-for="(item, index) in res" :key="index">
          <td>{{ item.date }}</td>
          <td>{{ item.doctor }}</td>
          <td>{{ item.diagnosis }}</td>
          <td>{{ item.prescription }}</td>
          <td @click="fn(item.doctor)">详情</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>
<script>
export default {
  name: "MyTable",
  data() {
    return {
      isShow: true,
      doctorInfo: "",
    };
  },
  props: {
    list: {
      type: Array,
      required: true,
    },
  },
  methods: {
    handleFilter() {
      if(this.doctorInfo.trim()==="") return
      this.$emit("handleFilter", this.doctorInfo);
      this.isShow = false;
      this.res = this.list.filter((item) => item.doctor === this.doctorInfo);
      if(this.res.length===0) alert("查无此人")
    },
    fn(doctor) {
      this.$emit("fn", doctor);
    },
    fnInput() {
      if (this.doctorInfo === "") {
        this.isShow = true;
      }
    },
  },
};
</script>

<style scoped>
.my-table {
  border-collapse: collapse;
  width: 100%;
}

.my-table td,
.my-table th {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}

.my-table th {
  background-color: #f2f2f2;
}
</style>
